package org.come.login;

import org.come.bean.AccountBinding;
import org.come.entity.AreaQuery;
import org.come.entity.RegionResult;
import org.come.socket.Agreement;
import org.come.socket.GameClient;
import org.come.socket.SendMessageUntil;
import org.come.test.Main;
import org.come.until.GsonUtil;
import org.come.until.MessagrFlagUntil;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URI;
import java.util.List;

public class LoginMouslisten implements MouseListener {

    private int i;
    private SpriteBtn btn;
    private LoginJpanel loginJpanel;

    // -1踢出 0"记住账号" 1"密码保护" 2"注册" 3"改密" 4"下拉" 5"登录" 6"立即注册" 7验证码确认
    public LoginMouslisten(int i, SpriteBtn btn, LoginJpanel loginJpanel) {
        super();
        this.i = i;
        this.btn = btn;
        this.loginJpanel = loginJpanel;
    }
    // -1踢出 0"记住账号" 1"密码保护" 2"注册" 3"改密" 4"下拉" 5"登录" 6"立即注册" 7验证码确认

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // 点击
        if (btn.isChoose()) {
            if (btn.getZhen() != 2) {// 选择类 选中
                btn.btn(2);
                if (i == 0) {// 记住账号
                    SpriteBtn btn = new SpriteBtn("resource/NewUi/按钮_勾选.钩子", 399, 378, true);
                    btn.setBounds(399, 378, 19, 19);
                    loginJpanel.getLoginView().setBtngouzi1(btn);
                } else if (i == 1) {// 密码保护
                    SpriteBtn btn = new SpriteBtn("resource/NewUi/按钮_勾选.钩子", 582, 490, true);
                    btn.setBounds(582, 490, 19, 19);
                    loginJpanel.getLoginView().setBtngouzi2(btn);
                    KeyView keyView = loginJpanel.getLoginView().getKeyView();
                    if (keyView == null) {
                        keyView = new KeyView(loginJpanel.getLoginView());
                        keyView.setBounds(170, 460, 700, 200);
                        loginJpanel.getLoginView().setKeyView(keyView);
                        loginJpanel.getLoginView().add(keyView, 0);
                    }
                    keyView.setVisible(true);
                }
            } else {
                // 选择类 取消选中
                btn.btn(0);
                if (i == 0) {// 记住账号
                    loginJpanel.getLoginView().setBtngouzi1(null);
                } else if (i == 1) {// 密码保护
                    loginJpanel.getLoginView().setBtngouzi2(null);
                    KeyView keyView = loginJpanel.getLoginView().getKeyView();
                    if (keyView != null) {
                        keyView.setVisible(false);
                    }
                }
            }
        } else {
            btn.btn(2);
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        // 鼠标释放
        if (!btn.isChoose()) {// 非选择类生效
            btn.btn(0);
            if (i == -1) {// 踢出
                System.exit(0);
            } else if (i == 2) {// 注冊
                RegisterView registerView = loginJpanel.getLoginView().getRegisterView();
                if (registerView == null) {
                    registerView = new RegisterView(loginJpanel);
                    registerView.setBounds(50, 100, 424, 406);
                    loginJpanel.getLoginView().setRegisterView(registerView);
                    loginJpanel.getLoginView().add(registerView, 0);
                } else {
                    registerView.setVisible(!registerView.isVisible());
                }
            } else if (i == 3) {// 改密
                try {// 创建URI统一资源标识符
                    Desktop desktop = Desktop.getDesktop();
                    URI uri = new URI("" + GameClient.potAndIpStrings[5]);
                    desktop.browse(uri);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            } else if (i == 4) {// 下拉
                // CreateRoleJpanel.dropdownJfram.setVisible(!CreateRoleJpanel.dropdownJfram.isVisible());
                DownView downView = loginJpanel.getLoginView().getDownView();
                if (downView == null) {
                    downView = new DownView(loginJpanel);
                    downView.setBounds(398, 340, 210, 122);
                    loginJpanel.getLoginView().setDownView(downView);
                    loginJpanel.getLoginView().add(downView, 0);
                } else {
                    downView.setVisible(!downView.isVisible());
                }
            } else if (i == 5) {// 登录
                loginJpanel.getLoginView().login();
            } else if (i == 6) {// 注册
                loginJpanel.getLoginView().getRegisterView().zc();
            } else if (i == 7) {// 验证码验证
                IphoneView iphoneView = loginJpanel.getLoginView().getIphoneView();
                if (iphoneView.getArrBean().equals(iphoneView.getVerificationText().getText())) {
                    // Main.frame.getLoginJpanel().loginSuccess(iphoneView.getArrBean());
                    /** HGC-2020-02-03 */
                    Main.frame.getLoginJpanel().setLogin_uid(GameClient.userid);
                    AreaQuery areaQuery = new AreaQuery();
                    areaQuery.setRe_id("");
                    areaQuery.setReName("");
                    areaQuery.setUserId(GameClient.userid);
                    String sendmes = Agreement.getAgreement().getareaAgreement(GsonUtil.getGsonUtil().getgson().toJson(areaQuery));
                    SendMessageUntil.toServer(sendmes);
                } else {
                    iphoneView.getLabMsgTip().setText("验证码错误");
                }
            }
            /** zrikka 2020 0415 */
            else if (i == 8) {
                // 提示框确定
                BindAccountView accountView = loginJpanel.getAreaView().getAccountView();
                if (accountView == null) {
                    accountView = new BindAccountView(loginJpanel);
                    accountView.setBounds(150, 220, 424, 278);
                    loginJpanel.getAreaView().setAccountView(accountView);
                    loginJpanel.getAreaView().add(accountView, 0);
                } else {
                    accountView.setVisible(true);
                }
                loginJpanel.getAreaView().getAccountTipView().setVisible(false);
            } else if (i == 9) {
                // 提示框取消
                AccountBinding accountBinding = new AccountBinding();
                accountBinding.setType("autoBinding");
                accountBinding.setUsername(SendMessageUntil.account.getAc_account());// 账号
                accountBinding.setPassword(SendMessageUntil.account.getAc_pasw());// 密码
                accountBinding.setSafely(SendMessageUntil.account.getAc_safely());// 安全码
                accountBinding.setTuiji(SendMessageUntil.account.getAc_tuijian());// 推荐码
                accountBinding.setFlag(SendMessageUntil.account.getAc_flag());// 账号标识
                accountBinding.setPhone(SendMessageUntil.account.getAc_phone());// 手机号
                String res = GsonUtil.getGsonUtil().getgson().toJson(accountBinding);
                // 获取账号的绑定信息
                String content = Agreement.getAgreement().Account_BindingAgreement(res);
                SendMessageUntil.toServer(content);
            } else if (i == 10) {
                // 绑定确定
                BindAccountView accountView = loginJpanel.getAreaView().getAccountView();
                String username = accountView.getTextAcc().getText();
                String password = new String(accountView.getTextPwd().getText());
                String safety = new String(accountView.getTextScode().getText());

                AccountBinding accountBinding = new AccountBinding();
                accountBinding.setType("binding");
                accountBinding.setUsername(username);
                accountBinding.setPassword(password);
                accountBinding.setSafely(safety);
                accountBinding.setFlag(SendMessageUntil.account.getAc_flag());

                String res = GsonUtil.getGsonUtil().getgson().toJson(accountBinding);
                String content = Agreement.getAgreement().Account_BindingAgreement(res);
                SendMessageUntil.toServer(content);
            } else if (i == 11) {
                // 绑定取消
                BindAccountView accountView = loginJpanel.getAreaView().getAccountView();
                if (accountView == null) {
                    accountView = new BindAccountView(loginJpanel);
                    accountView.setBounds(150, 220, 424, 278);
                    loginJpanel.getAreaView().setAccountView(accountView);
                    loginJpanel.getAreaView().add(accountView, 0);
                }
                accountView.setVisible(false);
            } else if (i == 99) {
                List<RegionResult> results = loginJpanel.regionResultList;
                loginJpanel.framechange(5, results, null);
            }
            /** zrikka 2020 0415 */
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE12);
        // TODO Auto-generated method stub
        if (btn.getZhen() != 2) {
            btn.btn(1);
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
        MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
        // TODO Auto-generated method stub
        if (btn.getZhen() != 2) {
            btn.btn(0);
        }
    }
}
